/**
 * 
 */
package com.netjava.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.netjava.pojo.AucRecord;
import com.netjava.pojo.Auctioning;
import com.netjava.pojo.Product;
import com.netjava.service.IAucRecordService;
import com.netjava.service.IAuctioningService;
import com.netjava.service.IProductService;

/**
 * @author chenchaohui
 *
 * 夺金网
 */
public class UpdateRecAction extends BaseAction {

	/**
	 * 同步更新商品详情页面竞拍的记录，以及时间
	 * 
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		 response.setContentType("text/xml;charset=UTF-8"); 
		 response.setHeader("Cache-Control", "no-cache");
		 String id=request.getParameter("auctioningId");
		    int auctionId=Integer.parseInt(id);
		 
		 
		 IAucRecordService aucRecordService=(IAucRecordService)getBean("aucRecordService");
		    
		  //通过产品id找到相应的产品
			IProductService productService=(IProductService)getBean("productService");
			Product product = productService.getProById(auctionId);
		    List<AucRecord> aucRecords = aucRecordService.getByProId(product);
		    
		  //只取最新的10条记录在页面显示
			List<AucRecord> records=new ArrayList();
			if(aucRecords.size()<10){
			for(int i=0;i<aucRecords.size();i++){
				records.add(aucRecords.get(i));
			}
			}
			else if(aucRecords.size()>=10){
				for(int i=0;i<10;i++){
					records.add(aucRecords.get(i));
				}
			}
			PrintWriter out = response.getWriter();
	        //要输出的xml文档内容
			String xml;
			xml="<acuRecords>  ";
			for(int i=0;i<records.size();i++){
				AucRecord aucRecord = records.get(i);
				xml+=" <acurecord>   "
					+"   <price>"
					+aucRecord.getAucPrice()
					+"   </price>    "
					+"   <userName>  "
					+aucRecord.getUser().getUserName()
					+"   </userName> "
					+"   <time>      "
					+aucRecord.getAucTime()
					+"   </time>     "
					+" </acurecord>  ";
			}
			
			
		    
		    IAuctioningService service=(IAuctioningService)getBean("auctioningService");
		    Auctioning auctioning = service.getAuctioningById(auctionId);
		    String remainTime=auctioning.getRemainTime();
		    xml+="<remainTime>"
		    	+remainTime
		    	+"</remainTime>";
		    
		    xml+="</acuRecords>  ";
			
			out.print(xml);
			out.flush();
			out.close();
			
		return null; 
	}

}
